package com.dungeon.game;

import java.util.Arrays;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dungeon.game.Constants.Game_mode;
import com.dungeon.game.Constants.Monster_code;
import com.dungeon.game.Constants.Position_code;
import com.dungeon.game.Enemies.Mob;

public class EnemyPane extends View {

	public EnemyPane(Context context) {
		super(context);
		init();
		init_detector(context);
	}

	public EnemyPane(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		init_detector(context);
	}

	public EnemyPane(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		init_detector(context);
	}

	public MyGestureListener listener;
	private GestureDetector detector;

	public void init_detector(Context context){
		listener = new MyGestureListener(context);
		detector = new GestureDetector(context,listener);
	}

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    	MainActivity activity;
    	Context context;

    	public MyGestureListener(Context _context){
    		super();
    		context = _context;
    	}

    	public boolean onDown(MotionEvent e) {
            return true;
        }

        public boolean onDoubleTap(MotionEvent event) {
        	// havent been getting far with this
            return true;
        }

        public boolean onSingleTapUp(MotionEvent event) {
        	Gamestate.dragging = null;
        	if (Gamestate.mode==Game_mode.MODE_NORMAL){
        		// pass
    		} else if (Gamestate.mode == Game_mode.MODE_ENEMY_TARGET){
        		Mob potential_target = null;
            	for (Position_code pos : Arrays.asList(Position_code.POS_M_BACK_1,Position_code.POS_M_BACK_2,Position_code.POS_M_BACK_3,Position_code.POS_M_FRONT_1,Position_code.POS_M_FRONT_2,Position_code.POS_M_FRONT_3)){
            		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
            			Mob m = Gamestate.e.get_monster_at_pos(pos);
            			if (m!=null && m.alive && !m.about_to_die){
            				potential_target = m;
            			}
            		}
            	}
            	if (potential_target!=null){
            		if (Gamestate.action.enemy_target_ok(potential_target)){
            			Gamestate.action.mob_target = potential_target;
            			Gamestate.action.trigger();
            		}
            	}
        		Gamestate.mode=Game_mode.MODE_NORMAL;
        	} else {
        		Gamestate.mode=Game_mode.MODE_NORMAL;
          	}
        	return false;
        }

        public void onLongPress(MotionEvent event) {
    		Mob md = null;
    		for (Position_code pos : Arrays.asList(Position_code.POS_M_BACK_1,Position_code.POS_M_BACK_2,Position_code.POS_M_BACK_3,Position_code.POS_M_FRONT_1,Position_code.POS_M_FRONT_2,Position_code.POS_M_FRONT_3)){
    			if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
    				Mob m = Gamestate.e.get_monster_at_pos(pos);
    				if (m!=null && m.alive){
    					md = m;
    				}
    			}
    		}
    		if (md!=null){
    	        AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	        ScrollView sv = new ScrollView(context);
    	        sv.setBackgroundColor(Color.argb(0,0,0,0));
    	        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    	        LinearLayout ll = new LinearLayout(context);
    	        ll.setBackgroundColor(Color.argb(0,0,0,0));
    	        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    	        ll.setOrientation(LinearLayout.VERTICAL);
    	        ll.setGravity(Gravity.TOP);
    	        if (md.description.equals("")){
    	        	ll.addView(onetext(context,""));
    	        } else {
    	        	ll.addView(onetext(context,md.description));
    	        }
    	        ll.addView(heading(context,"Status"));
    	        ll.addView(twotext(context,"Health:",md.health + " / " + md.max_health));
    	        if (md.rallied_until > Gamestate.w.clock)
    	        	ll.addView(onetext_italic(context,"This monster has been ralled for a temporary boost"));
    	        if (md.confused)
    	        	ll.addView(onetext_italic(context,"This monster seems confused"));
    	        if (md.stunned)
    	        	ll.addView(onetext_italic(context,"This monster is stunned"));
    	        if (md.slow_until > Gamestate.w.clock)
    	        	ll.addView(onetext_italic(context,"This monster is moving slowly"));
    	        if (md.vendetta)
    	        	ll.addView(onetext_italic(context,"Your party hates this monster"));
    	        if (md.evil)
    	        	ll.addView(onetext_italic(context,"This monster is evil"));
    	        ll.addView(heading(context,"Defenses"));
    	        ll.addView(twotext(context,"Armor:",md.current_armor()));
    	        ll.addView(twotext(context,"Evasion:",md.current_evasion()));
    	        if (md.poison_resistant)
    	        	ll.addView(onetext(context,"Poison resistant"));
    	        if (md.fire_resistant)
    	        	ll.addView(onetext(context,"Fire resistant"));
    	        if (md.cold_resistant)
    	        	ll.addView(onetext(context,"Cold resistant"));
    	        if (md.lightning_resistant)
    	        	ll.addView(onetext(context,"Lightning resistant"));
    	        if (md.stun_resistant)
    	        	ll.addView(onetext(context,"Stun resistant"));
    	        if (md.confusion_resistant)
    	        	ll.addView(onetext(context,"Confusion resistant"));
    	        if (md.ranged_only_evasion > 0)
    	        	ll.addView(onetext(context,"Protected from missiles"));
    	        if (md.melee_attack){
        	        ll.addView(heading(context,"Base melee attack"));
    	            if (md.poison_melee==1)
    	            	ll.addView(onetext_italic(context,"Poisonous"));
    	            if (md.poison_melee>=2)
    	            	ll.addView(onetext_italic(context,"Very poisonous!"));
    	            if (md.fire_melee)
    	            	ll.addView(onetext_italic(context,"Damage halved if you are fire resistant"));
    	            if (md.AP_melee)
    	            	ll.addView(onetext_italic(context,"Ignores armor"));
    	            ll.addView(twotext(context,"Accuracy:",md.accuracy));
    	            ll.addView(twotext(context,"Rate of:","1 per " + round_to_1_dp(md.melee_period/Constants.TIME_SCALAR) + " s"));
    	            if (md.enrage>0)
    	            	ll.addView(twotext(context,"Damage:",(md.melee_min_dam) + "-" + (md.melee_max_dam) + ", +" + ((md.enrage*md.enrage*10) + "% for enrage")));
    	            else
    	            	ll.addView(twotext(context,"Damage:",(md.melee_min_dam) + "-" + (md.melee_max_dam)));
    	        }
    	        if (md.missile_attack){
        	        ll.addView(heading(context,"Base missile attack"));
    	            ll.addView(twotext(context,"Aim:",md.aim));
    	            ll.addView(twotext(context,"Rate of:","1 per " + round_to_1_dp(md.missile_period/Constants.TIME_SCALAR) + " s"));
	            	ll.addView(twotext(context,"Damage:", (md.missile_min_dam) + "-" + (md.missile_max_dam)));
    	        }
    	        int n_actions = 0;
    	        for (Monster_action ma : md.actions){
    	        	if (!ma.get_description().equals("")){
    	        		n_actions = n_actions + 1;
    	        	}
    	        }
    	        if (n_actions>0 || md.split || md.regenerate){
        	        ll.addView(heading(context,"Abilities"));
    	            if (md.split)
    	            	ll.addView(onetext_italic(context,"It can split in half."));
    	            if (md.regenerate)
    	            	ll.addView(onetext_italic(context,"It regenerates its health."));
    	            if (md.speeds_time)
    	            	ll.addView(onetext_italic(context,"It makes time pass more quickly."));
    	            if (md.prevents_fleeing)
    	            	ll.addView(onetext_italic(context,"You cannot flee from it."));
    	            if (md.actions.size()>0){
    	                for (Monster_action action : md.actions){
    	                	if (!(action.get_description().equals(""))){
    	                		ll.addView(twotext_firstitalic(context,action.name(),action.get_description()));
    	                	}
    	                }
    	            }
    	        }
    	        sv.addView(ll);
    	        builder.setTitle(md.name)
    	            .setCancelable(true)
    	            .setOnCancelListener(new OnCancelListener() {public void onCancel(DialogInterface dialog) {activity.unpause_for_dialog();}})
    	            .setIcon(new BitmapDrawable(getResources(),LaunchActivity.gi.getImageBasic(md.image)))
    	            .setView(sv)
    	            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	            	public void onClick(DialogInterface dialog, int id) {
    	            		dialog.cancel();}});
       	        activity.pause_for_dialog();
       	        builder.create().show();
    		}
        }
    }

    public View onetext(Context context, String txt){
        TextView t = new TextView(context);
        t.setText(txt);
        return t;
    }

    public View onetext_italic(Context context, String txt){
        TextView t = new TextView(context);
        t.setText(txt);
        t.setTypeface(t.getTypeface(), Typeface.ITALIC);
        return t;
    }

    public View heading(Context context, String txt){
        TextView head1 = new TextView(context);
        head1.setText(txt);
        head1.setTypeface(head1.getTypeface(), Typeface.BOLD);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        llp.setMargins(0, 16, 0, 0);
        head1.setLayoutParams(llp);
        return head1;
    }

    public View twotext_firstitalic(Context context,String txt1,String txt2){
        LinearLayout row1 = new LinearLayout(context);
        row1.setBackgroundColor(Color.argb(0,0,0,0));
        row1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        row1.setOrientation(LinearLayout.HORIZONTAL);
        row1.setGravity(Gravity.LEFT);
        TextView item1 = new TextView(context);
        item1.setText(txt1);
        item1.setWidth(110);
        item1.setTypeface(item1.getTypeface(), Typeface.ITALIC);
        row1.addView(item1);
        TextView item2 = new TextView(context);
        item2.setText(txt2);
        row1.addView(item2);
        return row1;
    }

    public View twotext(Context context,String txt1,String txt2){
        LinearLayout row1 = new LinearLayout(context);
        row1.setBackgroundColor(Color.argb(0,0,0,0));
        row1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        row1.setOrientation(LinearLayout.HORIZONTAL);
        row1.setGravity(Gravity.LEFT);
        TextView item1 = new TextView(context);
        item1.setText(txt1);
        item1.setWidth(110);
        row1.addView(item1);
        TextView item2 = new TextView(context);
        item2.setText(txt2);
        row1.addView(item2);
        return row1;
    }

    public View twotext(Context context,String txt1,int txt2){
    	return twotext(context,txt1,Integer.toString(txt2));
    }

    static double round_to_1_dp(double x){
        return Math.round(x*10.0)/10.0;
    }

    public boolean onTouchEvent(MotionEvent event) {
	  return detector.onTouchEvent(event);
	}

	void safeDrawBitmap(Canvas canvas,Bitmap b,Rect r){
		if (b!=null && r!=null){
			canvas.drawBitmap(b,null,r,null);
		}
	}

	HashMap<Position_code,Rect> pos_to_rect, pos_to_tall_rect, pos_to_health_bar_rect;
	Paint screenpaint;

	void init(){
	    pos_to_rect = new HashMap<Position_code,Rect>();
	    pos_to_rect.put(Position_code.POS_M_BACK_1, new Rect(7, 62, 102, 157));
	    pos_to_rect.put(Position_code.POS_M_BACK_2, new Rect(135, 77, 230, 172));
	    pos_to_rect.put(Position_code.POS_M_BACK_3, new Rect(263, 62, 358, 157));
	    pos_to_rect.put(Position_code.POS_M_FRONT_1, new Rect(7, 176, 102, 271));
	    pos_to_rect.put(Position_code.POS_M_FRONT_2, new Rect(135, 191, 230, 286));
	    pos_to_rect.put(Position_code.POS_M_FRONT_3, new Rect(263, 176, 358, 271));
	    pos_to_rect.put(Position_code.POS_BACK_LEFT, new Rect(-100, -104, -5, -9));
	    pos_to_rect.put(Position_code.POS_BACK_RIGHT, new Rect(370, -104, 465, -9));
	    pos_to_tall_rect = new HashMap<Position_code,Rect>();
	    pos_to_tall_rect.put(Position_code.POS_M_BACK_1, new Rect(7, -34, 102, 157));
	    pos_to_tall_rect.put(Position_code.POS_M_BACK_2, new Rect(135, -19, 230, 172));
	    pos_to_tall_rect.put(Position_code.POS_M_BACK_3, new Rect(263, -34, 358, 157));
	    pos_to_tall_rect.put(Position_code.POS_M_FRONT_1, new Rect(7, 80, 102, 271));
	    pos_to_tall_rect.put(Position_code.POS_M_FRONT_2, new Rect(135, 95, 230, 286));
	    pos_to_tall_rect.put(Position_code.POS_M_FRONT_3, new Rect(263, 80, 358, 271));
	    pos_to_tall_rect.put(Position_code.POS_BACK_LEFT, new Rect(-100, -200, 91, -9));
	    pos_to_tall_rect.put(Position_code.POS_BACK_RIGHT, new Rect(370, -200, 561, -9));
	    pos_to_health_bar_rect = new HashMap<Position_code,Rect>();
	    pos_to_health_bar_rect.put(Position_code.POS_M_BACK_1, new Rect(107, 62, 114, 157));
	    pos_to_health_bar_rect.put(Position_code.POS_M_BACK_2, new Rect(235, 77, 242, 172));
	    pos_to_health_bar_rect.put(Position_code.POS_M_BACK_3, new Rect(363, 62, 370, 157));
	    pos_to_health_bar_rect.put(Position_code.POS_M_FRONT_1, new Rect(107, 176, 114, 271));
	    pos_to_health_bar_rect.put(Position_code.POS_M_FRONT_2, new Rect(235, 191, 242, 286));
	    pos_to_health_bar_rect.put(Position_code.POS_M_FRONT_3, new Rect(363, 176, 370, 271));
	    screenpaint = new Paint();
	    screenpaint.setStyle(Paint.Style.FILL);
	}

	protected void onDraw(Canvas canvas) {

	    if (!Gamestate.locked){
	    	Gamestate.locked = true;

			super.onDraw(canvas);
		    Paint paint = new Paint();
		    paint.setStyle(Paint.Style.FILL);
		    paint.setColor(Color.rgb(35, 35, 35));
		    canvas.drawPaint(paint);

	        for (Position_code pos : Arrays.asList(Position_code.POS_M_BACK_1,Position_code.POS_M_BACK_2,Position_code.POS_M_BACK_3,Position_code.POS_M_FRONT_1,Position_code.POS_M_FRONT_2,Position_code.POS_M_FRONT_3)){
	            Mob m = Gamestate.e.get_monster_at_pos(pos);
	            if (m!=null && m.alive){
		    		Rect r = new Rect(0,0,0,0);
		    		Rect small_r = new Rect(0,0,0,0);
	                if (m.slide_from==null || m.slide_finish < Gamestate.w.clock){
	                    small_r = pos_to_rect.get(pos);
	                    if (m.bigtile){
	                    	r = pos_to_tall_rect.get(pos);
	                    } else {
	                    	r = pos_to_rect.get(pos);
	                    }
	                } else {
	                    double proportion = (Gamestate.w.clock-m.slide_start)/(m.slide_finish-m.slide_start);
	                    Rect r1_small = pos_to_rect.get(m.slide_from);
	                    Rect r2_small = pos_to_rect.get(m.slide_to);
	                    Rect r1_tall = pos_to_tall_rect.get(m.slide_from);
	                    Rect r2_tall = pos_to_tall_rect.get(m.slide_to);
	                    small_r.left = (int)Math.round((1-proportion) * r1_small.left + (proportion) * r2_small.left);
	                    small_r.right = (int)Math.round((1-proportion) * r1_small.right + (proportion) * r2_small.right);
	                    small_r.bottom = (int)Math.round((1-proportion) * r1_small.bottom + (proportion) * r2_small.bottom);
	                    small_r.top = (int)Math.round((1-proportion) * r1_small.top + (proportion) * r2_small.top);
	                    if (m.bigtile){
	                        r.left = (int)Math.round((1-proportion) * r1_tall.left + (proportion) * r1_tall.left);
	                        r.right = (int)Math.round((1-proportion) * r1_tall.right + (proportion) * r1_tall.right);
	                        r.bottom = (int)Math.round((1-proportion) * r1_tall.bottom + (proportion) * r1_tall.bottom);
	                        r.top = (int)Math.round((1-proportion) * r1_tall.top + (proportion) * r1_tall.top);
	                    } else {
	                        r.left = (int)Math.round((1-proportion) * r1_small.left + (proportion) * r2_small.left);
	                        r.right = (int)Math.round((1-proportion) * r1_small.right + (proportion) * r2_small.right);
	                        r.bottom = (int)Math.round((1-proportion) * r1_small.bottom + (proportion) * r2_small.bottom);
	                        r.top = (int)Math.round((1-proportion) * r1_small.top + (proportion) * r2_small.top);
	                    }
	                }
	                if (!m.about_to_die){
	                    if (Gamestate.mode==Game_mode.MODE_ENEMY_TARGET && Gamestate.action.enemy_target_ok(m)){
		                    safeDrawBitmap(canvas,LaunchActivity.gi.getImageBasic("red_frame"),small_r);
	                    }
	                }
		            Bitmap b = null;
	                if (m.fade_in==false || m.fade_finish < Gamestate.w.clock){
	                    if (m.completely_invulnerable_until > Gamestate.w.clock || m.invulnerable_until > Gamestate.w.clock){
	                    	if (m.image.equals("turtloid")){
		                		b = LaunchActivity.gi.getImageBasic("turtloid_invulnerable");
			    			} else {
			    				b = LaunchActivity.gi.getImageWhite(m.image);
			    			}
		    			} else if (m.rallied_until > Gamestate.w.clock){
	                    	b = LaunchActivity.gi.getImageRed(m.image);
		    			} else if (m.waste_time_until > Gamestate.w.clock && m.code==Monster_code.MOB_SUMMONER){
		    				b = LaunchActivity.gi.getImageBasic("lazy_wizard_a_96_01");
		    			} else if (m.show_off_until > Gamestate.w.clock){
		    				b = LaunchActivity.gi.getImageBasic("vain_blue_dragon_a_96_01");
	                    } else {
		                    b = LaunchActivity.gi.getImageBasic(m.image);
		    			}
	                    safeDrawBitmap(canvas,b,r);
	                } else {
	                	// fading
	                	b = LaunchActivity.gi.getImageBasic(m.image);
	                    double proportion = (Gamestate.w.clock-m.fade_start)/(m.fade_finish-m.fade_start);
	                    Paint transparentpainthack = new Paint();
	                    transparentpainthack.setAlpha((int)(Math.floor(250*proportion)));
	                    if (b!=null && r!=null)
	                    	canvas.drawBitmap(b,null,r,transparentpainthack);
	                }
	                boolean colored = false;
	                if (m.flash_image!=null){
	                    if (m.flash_starts<0){
	                        m.flash_starts = Gamestate.w.clock;
	                    	Bitmap f = LaunchActivity.gi.getImageBasic(m.flash_image);
	                        safeDrawBitmap(canvas,f,r);
	                        if (r.height()==192 && (m.flash_image.equals("pale") || m.flash_image.equals("verypale"))){
	                            Rect second_box = new Rect(r.left,r.top-96,r.right,r.bottom);
	                            safeDrawBitmap(canvas,f,second_box);
	                        }
	                        colored = true;
	                    } else if (Gamestate.w.clock >= m.flash_starts + 0.1 && (m.flash_image.equals("pale") || m.flash_image.equals("verypale"))){
	                        m.flash_starts = -1;
	                        m.flash_image = null;
	                    } else if (Gamestate.w.clock >= m.flash_starts + 0.4){
	                        m.flash_starts = -1;
	                        m.flash_image = null;
	                    } else {
	                    	Bitmap f = LaunchActivity.gi.getImageBasic(m.flash_image);
	                        safeDrawBitmap(canvas,f,r);
	                        if (r.height()==192 && (m.flash_image.equals("pale") || m.flash_image.equals("verypale"))){
	                            Rect second_box = new Rect(r.left,r.top-96,r.right,r.bottom);
	                            safeDrawBitmap(canvas,f,second_box);
	                        }
	                        colored = true;
	                    }
	                }
	                if (m.flash_number>0 && m.flash_image==null){
	                    if (Gamestate.w.clock >= m.number_starts + 1.5){
	                        m.flash_number = -1;
	                        m.number_starts = -1;
	                    } else {
	                    	paint.setColor(m.number_color);
	                	    paint.setAlpha(240 - (int)Math.floor(160 * (Gamestate.w.clock - m.number_starts)));
	                	    paint.setTextSize(36);
	                	    paint.setAntiAlias(true);
	                	    paint.setTextAlign(Align.CENTER);
	                	    float width = paint.measureText(Integer.toString(m.flash_number));
	                	    canvas.drawText(Integer.toString(m.flash_number), small_r.left+48, small_r.top+47, paint);
	                    }
	                }
	                if (!colored){
	                    if (m.spotted_until>Gamestate.w.clock){
	                    	b = LaunchActivity.gi.getImageBasic("whitesquare");
	                        safeDrawBitmap(canvas,b,r);
	                        if (r.height()==192){
	                            Rect second_box = new Rect(r.left,r.top-96,r.right,r.bottom-96);
	                            safeDrawBitmap(canvas,b,second_box);
	                        }
	                    } else if ((m.slow_until>Gamestate.w.clock) || m.confused || m.stunned){
	                    	b = LaunchActivity.gi.getImageBasic("grey");
	                        safeDrawBitmap(canvas,b,r);
	                        if (r.height()==192){
	                            Rect second_box = new Rect(r.left,r.top-96,r.right,r.bottom-96);
	                            safeDrawBitmap(canvas,b,second_box);
	                        }
	                    }
	                }
	                if (m.about_to_die && m.flash_image==null){
	                	Bitmap sk = LaunchActivity.gi.getImageBasic("skull_crossbones");
	                	Rect r2 = new Rect(small_r.left+28,small_r.top+52,small_r.left+27+sk.getWidth(),small_r.top+51+sk.getHeight());
	                	safeDrawBitmap(canvas,sk,r2);
		    		}
	                if (m.slide_from==null || m.slide_finish < Gamestate.w.clock){
		    			Rect entire_health_rect = pos_to_health_bar_rect.get(pos);
		    			int health_color = Color.rgb(20,200,20);
		    			int health_height = (int)Math.ceil(m.health*(double)(entire_health_rect.height())/m.max_health);
		    			Rect used_health_rect = new Rect(entire_health_rect.left,entire_health_rect.bottom-health_height+1,entire_health_rect.right,entire_health_rect.bottom);
		    			paint.setColor(health_color);
		    			if (health_height>0)
		    				canvas.drawRect(used_health_rect, paint);
	                }
		    	}
		    }
	        for (int i=0; i<Gamestate.e.mobs.size(); i++){
	        	Mob m = Gamestate.e.mobs.get(i);
	            if (!m.alive && m.slide_from!=null && m.slide_finish >= Gamestate.w.clock){
		    		Rect r = new Rect(0,0,0,0);
	                double proportion = (Gamestate.w.clock-m.slide_start)/(m.slide_finish-m.slide_start);
	                Rect r1_small = pos_to_rect.get(m.slide_from);
	                Rect r2_small = pos_to_rect.get(m.slide_to);
	                Rect r1_tall = pos_to_tall_rect.get(m.slide_from);
	                Rect r2_tall = pos_to_tall_rect.get(m.slide_to);
	                if (m.bigtile){
	                    r.left = (int)Math.round((1-proportion) * r1_tall.left + (proportion) * r1_tall.left);
	                    r.right = (int)Math.round((1-proportion) * r1_tall.right + (proportion) * r1_tall.right);
	                    r.bottom = (int)Math.round((1-proportion) * r1_tall.bottom + (proportion) * r1_tall.bottom);
	                    r.top = (int)Math.round((1-proportion) * r1_tall.top + (proportion) * r1_tall.top);
	                } else {
	                    r.left = (int)Math.round((1-proportion) * r1_small.left + (proportion) * r2_small.left);
	                    r.right = (int)Math.round((1-proportion) * r1_small.right + (proportion) * r2_small.right);
	                    r.bottom = (int)Math.round((1-proportion) * r1_small.bottom + (proportion) * r2_small.bottom);
	                    r.top = (int)Math.round((1-proportion) * r1_small.top + (proportion) * r2_small.top);
	                }
		            Bitmap b = LaunchActivity.gi.getImageBasic(m.image);
		    		safeDrawBitmap(canvas,b,r);
	            }
	        }
	        for (int i=0; i<Gamestate.w.darkness; i++){
	    	    screenpaint.setColor(Color.BLACK);
	    	    screenpaint.setAlpha(85);
	    	    canvas.drawPaint(screenpaint);
	        }
	    	if (Gamestate.w.slow_time_until >= Gamestate.w.clock){
	    		screenpaint.setColor(Color.BLUE);
	    		screenpaint.setAlpha(50);
	    	    canvas.drawPaint(screenpaint);
	    	}
	    	if (Gamestate.w.dark_flare>0){
	    		if (Gamestate.w.dark_flare>Gamestate.w.clock){
		    		screenpaint.setColor(Color.BLACK);
		    		screenpaint.setAlpha(200);
		    	    canvas.drawPaint(screenpaint);	    			
	    		} else {
	    			Gamestate.w.dark_flare = 0;
	    		}
	    	}
	    	if (Gamestate.w.bright_flare>0){
	    		if (Gamestate.w.bright_flare>Gamestate.w.clock){
		    		screenpaint.setColor(Color.WHITE);
		    		screenpaint.setAlpha(200);
		    	    canvas.drawPaint(screenpaint);	    			
	    		} else {
	    			Gamestate.w.bright_flare = 0;
	    		}
	    	}
	    	Gamestate.locked = false;
		}
	}
}
